package com.hzd.service;

import ablaze.framework.api.result.ExecuteResult;
import ablaze.framework.security.verifycode.VerifyCodeService;
import ablaze.framework.service.BaseService;
import com.hzd.condition.LandlordCondition;
import com.hzd.entity.Landlord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Kent
 * @date 2015/8/24.
 */
@Component
@Transactional
public class LandlordService extends BaseService {

    @Autowired
    private VerifyCodeService verifyCodeService;

    public ExecuteResult saveOrUpdate(Landlord entity){

        if (isEmpty(entity)){
            return failParamNull();
        }

        if(isEmpty(entity.getOpenid())){
            return failParamNull("请在微信浏览器中打开");
        }

        if (entityValidate(entity).fail()){
            return failParamInvalidate(entityValidate(entity).errorString());
        }

        if (!verifyCodeService.validate(entity.getMobileNo(),entity.getVerifyCode())){
            return failParamInvalidate("验证码不正确");
        }

        return success(storeAndQuery(entity));
    }

    public List<Landlord> findAll(LandlordCondition condition){

        if (isEmpty(condition)){
            return find(Landlord.class).findList();
        }

        return find(Landlord.class)
                .where()
                .contains("name",condition.getName())
                .eq("idNo",condition.getIdNo())
                .eq("mobileNo",condition.getMobileNo())
                .findList();
    }
}
